package com.taptech.doufu.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import com.taptech.doufu.ui.adapter.TTHomeNovelAdapter;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.MessageHandler;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.StartActivityUtil;

public class HomeNovelListView extends PullToRefreshListView implements MessageHandler
{
    private float xDistance, yDistance, xLast, yLast;
    private TTHomeNovelAdapter mAdapter;

    public HomeNovelListView(Context context)
    {
        super(context);
        init(context);
    }

    public HomeNovelListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    public HomeNovelListView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

    }

    private void init(Context context)
    {

        this.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, final int position, long arg3)
            {
                try
                {
                    HomeTopBean bean = mAdapter.getBean(position - 1);
                    if (bean != null)
                    {
                        if (DiaobaoUtil.String2Int(bean.getTopic_type()) == Constant.OBJECT_TYPE_NOVEL)
                        {
                            StartActivityUtil.novelOnclick(getContext(), bean.getId());
                        }
                    }

                } catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        });
    }

    public void setAdapter(TTHomeNovelAdapter adapter)
    {
        mAdapter = (TTHomeNovelAdapter) adapter;
        super.setAdapter(adapter);
    }

    //	public TTHomeNovelAdapter getAdapter() {
    //		return mAdapter;
    //	}


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        switch (ev.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;

                if (xDistance > yDistance)
                {
                    return false;
                }
        }

        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }

        return super.onTouchEvent(event);
    }


    @Override
    public void handleMessage(Object message)
    {
        try
        {
            if (message != null && message instanceof HomeTopBean)
            {
                HomeTopBean dataBean = (HomeTopBean) message;
                for (int i = 0; i < getChildCount(); i++)
                {
                    View view = getChildAt(i);
                    ContentViewHolder holder = (ContentViewHolder) view.getTag();
                    if (holder == null)
                        continue;
                    HomeTopBean sourceBean = holder.homeBean;
                    if (sourceBean == null || sourceBean.getId() == null)
                        continue;
                    if (sourceBean.getId().equals(dataBean.getId()))
                    {
                        int objType = DiaobaoUtil.String2Int(sourceBean.getObject_type());
                        int topicType = DiaobaoUtil.String2Int(sourceBean.getTopic_type());
                        if (objType == Constant.CONTENT_TYPE_TOPIC && (topicType == Constant.OBJECT_TYPE_USERS_TOPIC || topicType == Constant.OBJECT_TYPE_LIVE_TOPIC))
                        {
                            sourceBean.setArticle_num(dataBean.getArticle_num());
                            holder.commentCount.setText(DiaobaoUtil.String2Int(sourceBean.getArticle_num()) + "评论");
                            return;
                        }
                        sourceBean.getActionMsg().setComment_times(dataBean.getActionMsg().getComment_times());
                        holder.commentCount.setText(DiaobaoUtil.String2Int(sourceBean.getActionMsg().getComment_times()) + "评论");
                        return;

                    }
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public class ContentViewHolder {
        public HomeTopBean homeBean;
        public TextView commentCount;
    }
}
